<template>
  <div>
    <div style="display: flex;justify-content: flex-end;">
      <Button style="margin:2px 12px 10px;" type="primary" @click="createRecruit">新增内推</Button>
    </div>

    <can-edit-table
      refs="table"
      v-model="dataList"
      @on-delete="handleDel"
      @on-cell-change="handleCellChange"
      @on-change="handleChanging"
      :hover-show="true"
      :editIncell="true"
      :columns-list="columnList">
    </can-edit-table>
    <Page style="float: right; margin:10px;" :current="current_num" :total="page_count" :pageSize="page_size" showElevator showSizer @on-page-size-change="handleSizeChange" @on-change="handleChangePage"></Page>


    <Modal v-model="modal_show" title="创建招聘计划" width="500" :mask-closable="false">

      <div style="text-align:center;">
        <Form ref="pushFormValidate" :model="pushFormValidate" :rules="pushRuleValidate" :label-width="70">
          <Form-item prop="name" label="姓名">
            <Input v-model="pushFormValidate.name" type="text" placeholder="请输入姓名"/>
          </Form-item>

          <Form-item prop="user_email" label="邮箱">
            <Input v-model="pushFormValidate.user_email" type="text" placeholder="请输入邮箱"/>
          </Form-item>

          <Form-item prop="job_name" label="岗位">
            <Input v-model="pushFormValidate.job_name" type="text" placeholder="请输入岗位名称"/>
          </Form-item>

          <Form-item prop="remark" label="备注">
            <Input v-model="pushFormValidate.remark" type="textarea" placeholder="请输入备注"/>
          </Form-item>
        </Form>

      </div>

      <div slot="footer">
        <Button type="primary" size="large" long @click="submitCreate('pushFormValidate')">提交</Button>
      </div>
    </Modal>

  </div>
</template>
<script>
  import canEditTable from './part/canEditTable.vue';
  import pushApi from '../api/axios/pushApi'

  export default {
    components: {
      canEditTable
    },
    data () {
      return {
        modal_show: false,
        current_num: 1,
        page_count: 5,
        page_size: 5,
        pushFormValidate: {
          name: '',
          user_email: '',
          job_name: '',
          remark: ''
        },
        pushRuleValidate: {
          name: [
            {required: true, message: '姓名不能为空', trigger: 'blur'}
          ],
          user_email: [
            {required: true, message: '姓名不能为空', trigger: 'blur'}
          ],
          job_name: [
            {required: true, message: '岗位不能为空', trigger: 'blur'}
          ]
        },
        columnList: [
          {
            title: '序号',
            type: 'index',
            align: 'center'
          },
          {
            title: '姓名',
            key: 'name',
            sortable: true
          },
          {
            title: '邮箱',
            key: 'user_email',
            sortable: true
          },
          {
            title: '岗位',
            key: 'job_name',
            sortable: true
          },
          {
            title: '城市',
            key: 'city',
            sortable: true
          },
          {
            title: '招聘类型',
            key: 'recruit_type',
            sortable: true
          },
          {
            title: '进度',
            key: 'process_state',
            sortable: true
          },
          {
            title: '推荐人',
            key: 'recommend_staff',
            sortable: true
          },
          {
            title: '创建时间',
            key: 'create_time',
            sortable: true
          },
          {
            title: '备注',
            key: 'remark',
            sortable: true,
            editable: true
          },
          {
            title: '操作',
            align: 'center',
            width: 125,
            key: 'handle',
            handle: ['edit', 'delete']
          }
        ],
        dataList: []
      }
    },
    computed: {
      getStaffId (){
        return this.$store.getters['auth/getStaffId'];
      }
    },
    methods:{
      createRecruit (){
        this.modal_show = true;
      },
      submitCreate (pushFormValidate){
        this.$refs[pushFormValidate].validate((valid) => {
          if (valid) {
            this.modal_show = false;

            let that = this;
            pushApi.addPush({
              recommend_staff_id: that.getStaffId,
              name: that.pushFormValidate.name,
              user_email: that.pushFormValidate.user_email,
              job_name: that.pushFormValidate.job_name,
              remark: that.pushFormValidate.remark
            },{
              success (res) {
                if (res.data.status === 200) {
                  that.$Message.success("创建成功");
                }else {
                  console.log("addPush：" + res.data);
                }
              }
            });

          } else {
            this.$Message.error('请重新填写');
          }
        });
      },
      handleDel (val, index) {

        let that = this;
        pushApi.deletePush({
          pushId: val[index].recommendId
        },{
          success (res) {
            if (res.data.status === 200) {
              that.dataList.splice(index,1);
              that.$Message.success("删除成功");
            }else {
              that.$Message.error("删除失败");
              console.log("deletePush 请求失败：" + res.data);
            }
          }
        });

//        console.log(JSON.stringify(val));
//        this.$Message.success('删除了第' + (index + 1) + '行数据');
      },
      handleCellChange (val, index, key) {
        this.updatePush(val, index);
//        console.log(JSON.stringify(val));
//        this.$Message.success('修改了第 ' + (index + 1) + ' 行列名为 ' + key + ' 的数据');
      },
      handleChanging (val, index) {
        this.updatePush(val, index);
//        console.log(JSON.stringify(val));
//        this.$Message.success('修改了第' + (index + 1) + '行数据');
      },
      handleChangePage(pageNum){
        this.current_num = pageNum;
        this.updatePushList();

//        this.$Message.success(pageNum+"");
      },
      handleSizeChange(pageSize){
        this.page_size = pageSize;
        this.updatePushList();

//        this.$Message.success(pageSize+"");
      },
      updatePush(val, index){
        let that = this;
        pushApi.modifyPush({
          id: val[index].recommendId,
          remark: val[index].remark
        },{
          success (res) {
            if (res.data.status === 200) {
              that.$Message.success("修改成功");
            }else {
              that.$Message.error("修改失败");
              console.log("modifyPush 请求失败：" + res.data);
            }
          }
        });
      },
      updatePushList(){
        let that = this;
        pushApi.getPushList({
          currentPage: that.current_num,
          pageSize: that.page_size
        },{
          success (res) {
            if (res.data.status === 200) {
              that.dataList = res.data.data.list;
              that.page_count = res.data.data.count;
            }else {
              console.log("getPushList 请求失败：" + res.data);
            }
          }
        })
      }
    },
    created() {
      this.updatePushList();
    }
  }
</script>
<style>

</style>
